package com.lanhe.admin.controller.teacher;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.lanhe.common.annotation.Log;
import com.lanhe.common.core.controller.BaseController;
import com.lanhe.common.core.domain.AjaxResult;
import com.lanhe.common.core.page.TableDataInfo;
import com.lanhe.common.enums.BusinessType;
import com.lanhe.teacher.domain.LhGroup;
import com.lanhe.teacher.domain.vo.StatTeacherByGroupVo;
import com.lanhe.teacher.service.ILhGroupService;
import com.lanhe.teacher.service.ILhTeacherService;

/**
 * 组表(LhGroup)表控制层
 *
 * @author zhanggl
 * @since 2023-09-14 22:28:44
 */
@RestController
@RequestMapping("teacher/lhGroup")
public class LhGroupController extends BaseController {

    /**
     * 服务对象
     */
    @Resource
    private ILhGroupService lhGroupService;

    @Resource
    private ILhTeacherService teacherService;

    /**
     * 分页查询所有数据
     */
    @PreAuthorize("@ss.hasPermi('teacher:group:list')")
    @GetMapping("/list")
    public TableDataInfo list(LhGroup group) {
        startPage();
        List<LhGroup> list = lhGroupService.selectGroupList(group);
        return getDataTable(list);
    }

    /**
     * 根据组编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('teacher:group:query')")
    @GetMapping(value = { "/", "/{groupId}" })
    public AjaxResult getInfo(@PathVariable(value = "groupId") Long groupId)
    {
        return success(lhGroupService.selectGroupById(groupId));
    }

    /**
     * 新增小组
     */
    @PreAuthorize("@ss.hasPermi('teacher:group:add')")
    @Log(title = "小组管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody LhGroup group)
    {
        if (!lhGroupService.checkGroupNameUnique(group))
        {
            return error("新增'" + group.getGroupName() + "'失败，小组名称已存在");
        }
        group.setCreateBy(getUsername());
        return toAjax(lhGroupService.insertGroup(group));
    }

    /**
     * 修改小组
     */
    @PreAuthorize("@ss.hasPermi('teacher:group:edit')")
    @Log(title = "小组管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody LhGroup group)
    {
        if (ObjectUtils.isEmpty(group.getGroupId()) || null == group.getGroupId())
        {
            return error("小组主键ID不能为空");
        }
        if (!lhGroupService.checkGroupNameUnique(group))
        {
            return error("修改'" + group.getGroupName() + "'失败，小组名称已存在");
        }
        group.setUpdateBy(getUsername());
        return toAjax(lhGroupService.updateGroup(group));
    }

    /**
     * 删除小组
     */
    @PreAuthorize("@ss.hasPermi('teacher:group:remove')")
    @Log(title = "小组管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{groupIds}")
    public AjaxResult remove(@PathVariable Long[] groupIds)
    {
        List<LhGroup> lhGroups = lhGroupService.selectGroupByIds(groupIds);
        List<Long> groupId = lhGroups.stream().map(LhGroup::getGroupId).collect(Collectors.toList());


        // 检查是否有关联老师的小组
        List<StatTeacherByGroupVo> statTeacherByGroupVos = teacherService.statTeacherByGroupIds(groupId);

        for (StatTeacherByGroupVo statTeacherByGroupVo : statTeacherByGroupVos) {
            if (statTeacherByGroupVo.getNumber()>0) {
                return error("不能删除关联老师的小组");
            }
        }

        int deletedCount = lhGroupService.deleteGroupByIds(groupIds);
        if (deletedCount > 0) {
            return success("成功删除 " + deletedCount + " 个小组");
        } else {
            return error("没有删除任何小组");
        }
    }
}

